package com.uphone.kingmall.activity.personal.set;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.china.zhuoyue.R;
import com.kyleduo.switchbutton.SwitchButton;
import com.lzy.okgo.model.HttpParams;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.UserAddrBean;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.intent.IntentData;
import com.uphone.kingmall.view.SubmitButton;
import com.uphone.kingmall.view.cityseletor.CitySelectorUtil;
import com.uphone.kingmall.view.dialog.OnDialogViewClickListener;
import com.uphone.kingmall.view.dialog.RadishDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :编辑收货地址
 *     version: 1.0
 * </pre>
 */
public class EditAddressActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_area)
    TextView tvArea;
    @BindView(R.id.et_address_details)
    EditText etAddressDetails;
    @BindView(R.id.tv_delete)
    TextView tvDelete;
    @BindView(R.id.btn_save)
    SubmitButton btnSave;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.cb_default)
    SwitchButton cbDefault;
    @IntentData
    private UserAddrBean bean = new UserAddrBean();
    @IntentData
    private int id = -1;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_edit_address;
    }

    @Override
    public void initView() {
        btnSave.setRelaViews(etName, etPhone, tvArea, etAddressDetails);
        if (id == -1) {
            CommonUtil.initTitle(this, "添加收货地址", ll);
            tvDelete.setVisibility(View.GONE);
        } else {
            CommonUtil.initTitle(this, "编辑收货地址", ll);
            tvDelete.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initData() {
        if (id != -1) {
            etName.setText(bean.getReceiver() + "");
            etPhone.setText(bean.getTelephone() + "");
            tvArea.setText(bean.getRegion() + "");
            etAddressDetails.setText(bean.getAddress() + "");
            cbDefault.setChecked(bean.getBlDefault() == 1);
        }
    }


    @OnClick({R.id.tv_area, R.id.tv_delete, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_area:
                KeyboardUtils.hideSoftInput(this);
                CitySelectorUtil.citySelector(this, new CitySelectorUtil.CitySelectorCallBack() {
                    @Override
                    public void callBack(String province, String city, String district) {
                        if (TextUtils.isEmpty(province) || "北京".equals(province) || "天津".equals(province) ||
                                "上海".equals(province) || "重庆".equals(province) || "其它".equals(city)) {
                            tvArea.setText(city + district);
                        } else {
                            tvArea.setText(province + city + district);
                        }
                    }
                });
                break;
            case R.id.tv_delete:
                if (id != -1) {
                    showDialog();
                }
                break;
            case R.id.btn_save:
                saveAddress();
                break;
        }
    }

    private void showDialog() {
        new RadishDialog.Builder(this).setView(R.layout.kingmall_dialog_alter)
                .setText(R.id.dialog_message, "确定删除？")
                .setClick(R.id.dialog_submit, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(final RadishDialog dialog, View view) {
                        HttpParams params = new HttpParams();
                        params.put("addrId", id);
                        CommonUtil.submitNetAndFinish(EditAddressActivity.this, MyUrl.delUserAddr, params, "删除成功");
                        dialog.dismiss();
                    }
                })
                .setClick(R.id.dialog_cancel, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        dialog.dismiss();
                    }
                }).show();
    }

    private void saveAddress() {
        if (CommonUtil.checkViewEmpty(new String[]{"收货人不可为空", "手机号不可为空", "所选区域不可为空", "详细地址不可为空"}, etName, etPhone, tvArea, etAddressDetails)) {
            return;
        }
        HttpParams params = new HttpParams();
        params.put("receiver", etName.getText().toString().trim());
        params.put("telephone", etPhone.getText().toString().trim());
        params.put("region", tvArea.getText().toString().trim());
        params.put("address", etAddressDetails.getText().toString().trim());
        params.put("blDefault", cbDefault.isChecked() ? 1 : 0);

        if (id == -1) {
            //添加收货地址
            CommonUtil.submitNetAndFinish(this, MyUrl.addUserAddr, params, "保存成功");
        } else {
            //编辑收货地址
            params.put("addrId", id);
            CommonUtil.submitNetAndFinish(this, MyUrl.editUserAddr, params, "保存成功");
        }
    }
}
